package com.huilian.iotbox.admin.controller;


import com.huilian.iotbox.admin.service.AgencyService;
import com.huilian.iotbox.admin.service.UserService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.dto.AgencyDto;
import com.huilian.iotbox.data.exception.ControllerException;
import com.huilian.iotbox.data.po.Agency;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/agency")
public class AgencyController extends BaseController {

    @Resource
    private AgencyService agencyService;

    @Resource
    private UserService userService;

    @GetMapping("/list")
    public ServerResponse list(HttpServletRequest request, AgencyDto agencyDto) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        return agencyService.findListByKeyword(userInfo, agencyDto);
    }


    /**
     * 添加机构
     *
     * @param agencyDto
     * @return
     */
    @PostMapping
    public ServerResponse add(HttpServletRequest request, @RequestBody AgencyDto agencyDto) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        if (userInfo.getAgencyId().equals(agencyDto.getId())) {
            return ServerResponse.createByErrorMessage("机构不能修改自己的信息");
        }
        return agencyService.add(agencyDto, userInfo);
    }

    @GetMapping("/findAgency")
    public ServerResponse findAgency(HttpServletRequest request, Integer agencyId) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        return agencyService.findAgency(agencyId, userInfo);
    }


    /**
     * 修改机构自定义logo
     *
     * @param agencyDto
     * @return
     */
    @PutMapping("/updateCustomLogo")
    public ServerResponse updateCustomLogo(HttpServletRequest request, @RequestBody AgencyDto agencyDto) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        return agencyService.updateCustomLogo(agencyDto, userInfo);
    }

    /**
     * 验证营业执照在此机构内是否唯一
     *
     * @param bussAuthNum
     * @param agencyId
     * @return
     */
    @GetMapping("/checkBussAuthNum")
    public ServerResponse checkBussAuthNum(String bussAuthNum, Integer agencyId, HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        return agencyService.checkBussAuthNum(bussAuthNum, agencyId, userInfo);
    }


    /**
     * 验证简称是否唯一
     *
     * @param agency
     * @return
     */
    @GetMapping("/checkAbbreviation")
    public ServerResponse checkAbbreviation(Agency agency) {
        int count = agencyService.checkAbbreviation(agency.getAlias(), agency.getId());
        if (count > 0) {
            throw new ControllerException("机构简称已存在");
        } else {
            return ServerResponse.createBySuccess();
        }
    }


    @GetMapping("/getAllAgencyOfName")
    public ServerResponse getAllAgencyOfName(HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        return ServerResponse.createBySuccess(agencyService.getAllAgencyOfName(userInfo));
    }

    @GetMapping("/findAgencyByLoginUser")
    public ServerResponse findAgencyByLoginUser(HttpServletRequest request) {
        AgencyDto agencyDto = new AgencyDto();
        UserInfo userInfo = userService.getLoginUserInfo(request);
        agencyDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(agencyService.findAgencyByLoginUser(agencyDto));
    }

    @PostMapping("/changeAgencyStatus")
    public ServerResponse changeAgencyStatus(@RequestBody AgencyDto agencyDto, HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        agencyDto.setUserInfo(userInfo);
        return agencyService.changeAgencyStatus(agencyDto);
    }

    @GetMapping("/findDefaultBurningDeviceIdById")
    public ServerResponse findDefaultBurningDeviceIdById(HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        return ServerResponse.createBySuccess(agencyService.findDefaultBurningDeviceIdById(userInfo.getAgencyId()));
    }
}
